package com.zoe.aop.config;

import cn.hutool.core.util.StrUtil;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.interceptor.CustomizableTraceInterceptor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

/**
 * @author zoe.Zhang
 * @date 2023/03/01
 * @description
 */
@Aspect
@EnableAspectJAutoProxy(proxyTargetClass = true, exposeProxy = true)
@Configuration
public class AopConfig {
    @Pointcut("@annotation(com.zoe.aop.annotation.OperatorLog)")
    public void point() {
    }

    @Bean
    public Advisor advisor() {
        CustomizableTraceInterceptor customizableTraceInterceptor = new CustomizableTraceInterceptor();
        String method = StrUtil.format("类名-->{}.{}", CustomizableTraceInterceptor.PLACEHOLDER_TARGET_CLASS_SHORT_NAME, CustomizableTraceInterceptor.PLACEHOLDER_METHOD_NAME);
        customizableTraceInterceptor.setEnterMessage(method);
        String format = StrUtil.format("调用时间 --> {}", CustomizableTraceInterceptor.PLACEHOLDER_INVOCATION_TIME);
        customizableTraceInterceptor.setExitMessage(format);
        customizableTraceInterceptor.setUseDynamicLogger(true);
        AspectJExpressionPointcut aspectJExpressionPointcut = new AspectJExpressionPointcut();
        aspectJExpressionPointcut.setExpression("com.zoe.aop.config.AopConfig.point()");
        return new DefaultPointcutAdvisor(aspectJExpressionPointcut, customizableTraceInterceptor);
    }
}
